﻿using System;
using System.Globalization;
using System.IO;
using pub.Interop;
using pub.Settings;

namespace todo
{
    public class AppSettings : IniSettings
    {
        private bool _showMenu;
        private bool _progressVisible;
        private bool _descVisible;
        private bool _dueVisible;
        private bool _doneItemsVisible;
        private bool _showRibbonUI;
        private bool _gridLineVisible;
        private bool _treeLineVisible;
        private bool _headerVisible;
        private bool _topDownMode;
        private bool _openLastFileOnStart;
        private string _themeName;

        public AppSettings()
        {
            SetupIniFile();
            LoadSettings();
        }

        private void LoadSettings()
        {
            var preferenceSettings = this["Preference"];

            _showMenu = preferenceSettings["ShowMenu"].GetValue(1) == 1;
            _progressVisible = preferenceSettings["ProgressVisible"].GetValue(0) == 1;
            _descVisible = preferenceSettings["DescVisible"].GetValue(1) == 1;

            _dueVisible = preferenceSettings["DueVisible"].GetValue(0) == 1;
            _doneItemsVisible = preferenceSettings["DoneItemsVisible"].GetValue(1) == 1;
            _showRibbonUI = preferenceSettings["ShowRibbonUI"].GetValue(1) == 1;

            _gridLineVisible = preferenceSettings["GridLineVisible"].GetValue(1) == 1;
            _treeLineVisible = preferenceSettings["TreeLineVisible"].GetValue(0) == 1;
            _headerVisible = preferenceSettings["HeaderVisible"].GetValue(1) == 1;

            _topDownMode = preferenceSettings["TopDownMode"].GetValue(0) == 1;
            _openLastFileOnStart = preferenceSettings["OpenLastFileOnStart"].GetValue(0) == 1;

            _themeName = preferenceSettings["ThemeName"].GetValue("default");
        }

        private void SetupIniFile()
        {
            var iniFileName = Path.Combine(App.Path, "todo.ini");
            // test write permission, if not, change to users appdata folder
            if (!File.Exists(iniFileName))
            {
                var writable = Win32.WritePrivateProfileString("test", "time",
                        DateTime.Now.ToString(CultureInfo.InvariantCulture),
                        iniFileName);
                if (!writable)
                {
                    iniFileName = Path.Combine(App.DataPath, "todo.ini");
                }
                init(iniFileName);
            }
        }

        public bool ShowMenu
        {
            get { return _showMenu; }
            set
            {
                _showMenu = value;
                this["Preference"]["ShowMenu"].IntValue = value ? 1 : 0;
            }
        }

        public bool ProgressVisible
        {
            get { return _progressVisible; }
            set
            {
                _progressVisible = value;
                this["Preference"]["ProgressVisible"].IntValue = value ? 1 : 0;
            }
        }

        public bool DescVisible
        {
            get { return _descVisible; }
            set
            {
                _descVisible = value;
                this["Preference"]["DescVisible"].IntValue = value ? 1 : 0;
            }
        }

        public bool DueVisible
        {
            get { return _dueVisible; }
            set
            {
                _dueVisible = value;
                this["Preference"]["DueVisible"].IntValue = value ? 1 : 0;
            }
        }

        public string EmailTo
        {
            get { return this["Options"]["EmailTo"].StringValue; }
            set { this["Options"]["EmailTo"].StringValue = value; }
        }

        public bool DoneItemsVisible
        {
            get { return _doneItemsVisible; }
            set
            {
                _doneItemsVisible = value;
                this["Preference"]["DoneItemsVisible"].IntValue = value ? 1 : 0;
            }
        }

        public bool ShowRibbonUI
        {
            get { return _showRibbonUI; }
            set
            {
                _showRibbonUI = value;
                this["Preference"]["ShowRibbonUI"].IntValue = value ? 1 : 0;
            }
        }

        public bool GridLineVisible
        {
            get { return _gridLineVisible; }
            set
            {
                _gridLineVisible = value;
                this["Preference"]["GridLineVisible"].IntValue = value ? 1 : 0;
            }
        }

        public bool TreeLineVisible
        {
            get { return _treeLineVisible; }
            set
            {
                _treeLineVisible = value;
                this["Preference"]["TreeLineVisible"].IntValue = value ? 1 : 0;
            }
        }

        public bool HeaderVisible
        {
            get { return _headerVisible; }
            set
            {
                _headerVisible = value;
                this["Preference"]["HeaderVisible"].IntValue = value ? 1 : 0;
            }
        }

        public bool TopDownMode
        {
            get { return _topDownMode; }
            set
            {
                _topDownMode = value;
                this["Preference"]["TopDownMode"].IntValue = value ? 1 : 0;
            }
        }

        public bool OpenLastFileOnStart
        {
            get { return _openLastFileOnStart; }
            set
            {
                _openLastFileOnStart = value;
                this["Preference"]["OpenLastFileOnStart"].IntValue = value ? 1 : 0;
            }
        }

        public string ThemeName
        {
            get { return _themeName; }
            set
            {
                _themeName = value;
                this["Preference"]["ThemeName"].StringValue = value ?? "default";
            }
        }
    }
}